package com.example.koizzi.ad20180517_news;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.koizzi.ad20180517_news.bean.Collect;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.QueryListener;

public class LoginActivity extends AppCompatActivity {
    static boolean los = true;//是否登录
    @BindView(R.id.ed_tel)
    EditText edTel;
    @BindView(R.id.ed_code)
    EditText edCode;
    private String bbb;
    private String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }

    //dialog 按钮 手机号，验证码，登录
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_getCode:
                getCode();//通过手机号码获取验证码
                break;
            case R.id.btn_login:
                BmobLogin();//手机号码登录
                break;
        }
    }

    //手机号码登录
    private void BmobLogin() {
        bbb = getIntent().getStringExtra("BBB");
        id = getIntent().getStringExtra("OBJ");
        Toast.makeText(LoginActivity.this,bbb,Toast.LENGTH_SHORT).show();
        BmobUser.signOrLoginByMobilePhone(ReturnTelCode(true), ReturnTelCode(false), new LogInListener<BmobUser>() {
            @Override
            public void done(BmobUser bmobUser, BmobException e) {
                if (bmobUser != null) {
                    Toast.makeText(LoginActivity.this, "用户登录成功", Toast.LENGTH_SHORT).show();
                    los = true;
                    Intent info = new Intent(LoginActivity.this,ViewNewsActivity.class);
                    info.putExtra("TEL" , ReturnTelCode(true));
                    info.putExtra("NEWS_URL1" , bbb);
                    info.putExtra("OBJID" , id);
                    startActivity(info);
                } else {
                    Toast.makeText(LoginActivity.this,"未登录"+e.toString(),Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    //通过手机号码获取验证码
    private void getCode() {
        BmobSMS.requestSMSCode(ReturnTelCode(true), "VerCode", new QueryListener<Integer>() {
            @Override
            public void done(Integer integer, BmobException e) {
                if (e == null) {//验证码发送成功
                    Toast.makeText(LoginActivity.this, "验证码已发送", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(LoginActivity.this, "验证码发送失败" + e.toString(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * true 返回电话号码  false 返回验证码
     *
     * @param tel
     * @return
     */
    public String ReturnTelCode(boolean tel) {
        String phone = edTel.getText().toString();
        String code = edCode.getText().toString();
        if (tel) {
            return phone;
        } else {
            return code;
        }
    }

}
